08. Calculate a Covariance Matrix
Calculate a Covariance Matrix
Remember how we defined the covariance matrix:
And covariance is
If r_A and r_B are discrete vectors of values, that is, they can take on the values (r_{Ai}, r_{Bi}) for i = 1,\ldots, n , with equal probabilities 1/n, then the covariance can be equivalently written,
We use n-1 in the denominator of the constant for the same reason that we use n-1 in the denominator of the constant out front in the sample standard deviation—because we have a sample, and we want to calculate an unbiased estimate of the population covariance.
But if \bar{r}_A = \bar{r}_B = 0 , then the covariance equals
In matrix notation, this equals
Therefore, if \mathbf{r} is a matrix that contains the vectors \mathbf{r}_A and \mathbf{r}_B as its columns,
then
So if each vector of observations in your data matrix has mean 0, you can calculate the covariance matrix as: